package com.travel.server.manager.impl.command.order;

import com.travel.common.CommonConstant;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.order.TransferOrderRequest;
import com.travel.server.manager.dto.response.order.TransferOrderResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.OrderDal;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.impl.dal.SpecialDal;
import com.travel.server.manager.impl.enums.OrderStateEnum;
import com.travel.server.manager.impl.enums.OrderStepEnum;
import com.travel.server.manager.impl.sal.OrderSal;
import com.travel.server.manager.model.OrderInfo;
import com.travel.server.manager.model.ProjectInfo;
import com.travel.server.manager.model.ProjectSpecial;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/5 18:15
 * @Desc:
 */
@Component
@Slf4j
public class TransferOrderCommand extends AbstractCommand<TransferOrderRequest, TransferOrderResponse> {

    @Autowired
    private OrderDal orderDal;

    @Autowired
    private SpecialDal specialDal;

    @Autowired
    private ProjectDal projectDal;

    @Override
    protected void checkInput(TransferOrderRequest request) {
        if(request.getId() == null || request.getSpecialId() == null || StringUtils.isBlank(request.getNote())){
            throwInvalidParam();
        }
    }

    @Override
    public TransferOrderResponse handle(TransferOrderRequest request) {
        checkInput(request);

        UserInfo userInfo = getUserInfo();

        OrderInfo orderInfo = orderDal.selectById(request.getId());
        if(orderInfo.getTransferTime() != null && orderInfo.getTransferTime() > 1){
            log.info("订单已经转接过，无法转接， id {}", request.getId());
            throw new WorkException(WorkCode.ORDER_TRANSFER);
        }

        ProjectSpecial special = specialDal.selectById(request.getSpecialId());

        ProjectInfo projectInfo = projectDal.selectById(special.getProjectId());
        if(projectInfo == null){
            throw new WorkException(WorkCode.RECORD_NOT_EXIST);
        }
        Integer newId = makeTransfer(orderInfo, projectInfo, special, userInfo, request.getNote());
        TransferOrderResponse response = new TransferOrderResponse();
        response.setId(newId);
        return response;
    }

    public Integer makeTransfer(OrderInfo orderInfo, ProjectInfo projectInfo, ProjectSpecial special, UserInfo userInfo, String note){
        Date now = new Date();
        String newOrderId = OrderSal.getNewOrderId();
        //原订单显示备注+新单号
        orderInfo.setNote(OrderSal.getNewNote(orderInfo.getNote(), "新订单号：" + newOrderId + CommonConstant.SEMICOLON_STR + note));
        orderInfo.setTransferTime(orderInfo.getTransferTime() == null ? 0 : orderInfo.getTransferTime() + 1);
        orderInfo.setOrderState(OrderStateEnum.CLOSE.getState());
        orderInfo.setOrderStep(OrderStepEnum.END.getStep());
        orderInfo.setUpdateBy(userInfo.getName());
        orderInfo.setUpdateTime(now);

        orderDal.insertOrUpdate(orderInfo);

        //新增订单，订单信息与原订单一样
        OrderInfo newOrder = new OrderInfo();
        newOrder.setOrderId(newOrderId);
        newOrder.setOrderType(orderInfo.getOrderType());
        newOrder.setBuyerMessage(orderInfo.getBuyerMessage());
        newOrder.setUpdateBy(userInfo.getName());
        newOrder.setUpdateTime(now);
        newOrder.setSpecialId(special.getId());
        newOrder.setOrderAmount(special.getPrice());
        newOrder.setFreightAmount(orderInfo.getFreightAmount());
        newOrder.setOrderStep(OrderStepEnum.PROJECT_WAIT.getStep());
        newOrder.setOrderState(OrderStateEnum.WAIT_PAY.getState());
        newOrder.setIsTravelData(CommonConstant.NO);
        newOrder.setNote("原订单编号：" + orderInfo.getOrderId());
        newOrder.setCustomerId(orderInfo.getCustomerId());
        newOrder.setCreateBy(userInfo.getName());
        newOrder.setCreateTime(now);
        newOrder.setBuyerMessage(orderInfo.getBuyerMessage());
        newOrder.setCollectName(orderInfo.getCollectName());
        newOrder.setCollectAddress(orderInfo.getCollectAddress());
        newOrder.setCollectMobile(orderInfo.getCollectMobile());
        newOrder.setOrderTime(orderInfo.getOrderTime());
        newOrder.setTravelStartTime(special.getTravelDate());
        Integer day = CommonUtil.getDays(special.getDays());
        if(day != null){
            Date endDate = DateUtil.getDateNum(special.getTravelDate(), day);
            newOrder.setTravelEndTime(endDate);
        }
        newOrder.setOrderChargeId(projectInfo.getOrderChargeId());
        newOrder.setOrderChargeName(projectInfo.getOrderChargeName());
        newOrder.setDistributeType(orderInfo.getDistributeType());
        newOrder.setYouzanBirth(orderInfo.getYouzanBirth());
        newOrder.setYouzanMail(orderInfo.getYouzanMail());
        newOrder.setYouzanMobile(orderInfo.getYouzanMobile());
        newOrder.setYouzanName(orderInfo.getYouzanName());
        newOrder.setYouzanSex(orderInfo.getYouzanSex());
        newOrder.setYouzanWechat(orderInfo.getYouzanWechat());
        newOrder.setOrderTotal(orderInfo.getOrderTotal());
        newOrder.setTeamNumber(projectInfo.getNumber() + DateUtil.dateToStr(special.getTravelDate(), DateUtil.YYYYMMDD));

        return orderDal.insertOrUpdate(newOrder);
    }
}
